package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.DownloadDao;
import cz.khfotbal.domain.DownloadFile;

@Repository()
@Transactional
public class DownloadDaoImpl implements DownloadDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public void saveFile(DownloadFile downloadFile) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(downloadFile);
	}

	public List<DownloadFile> findAllDownloadFiles() {
		session = sessionFactory.getCurrentSession();
		List<DownloadFile> list = session.createQuery("from DownloadFile where category = 'file'")
				.list();
		return list;
	}
	
	public List<DownloadFile> findAllBanners() {
		session = sessionFactory.getCurrentSession();
		List<DownloadFile> list = session.createQuery("from DownloadFile where category = 'banner' order by id")
				.list();
		return list;
	}

	public void deleteDownloadFile(DownloadFile downloadFile) {
		session = sessionFactory.getCurrentSession();
		session.delete(downloadFile);

	}

}
